package com.auth.system.service.impl;

import com.auth.system.dao.SystemRoleMapper;
import com.auth.system.entity.build.SystemUserEntity;
import com.auth.system.entity.dto.SystemRoleDto;
import com.auth.system.service.UserService;
import com.auth.system.service.build.ISystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.utility.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author hewei
 * @since 2020-11-26
 */
@Service
public class UserImpl implements UserService {

    @Autowired private ISystemUserService iSystemUserService;

    @Autowired private SystemRoleMapper systemRoleMapper;


    /**
     * 根据用户名获取用户信息
     * @param userName 用户名
     * @return
     */
    @Override
    public SystemUserEntity getUser(String userName) {
        LambdaQueryWrapper<SystemUserEntity> userEntity = new LambdaQueryWrapper<>();
        userEntity.eq(SystemUserEntity::getUserName,userName);
        return iSystemUserService.getBaseMapper().selectOne(userEntity);
    }



    /**
     * 根据用户名 密码获取用户信息
     * @param userName 用户名
     * @param password  密码
     * @return
     */
    @Override
    public SystemUserEntity getUser(String userName, String password) {
        LambdaQueryWrapper<SystemUserEntity> userEntity = new LambdaQueryWrapper<>();
        userEntity.eq(SystemUserEntity::getUserName,userName);
        userEntity.eq(SystemUserEntity::getIsDelete,Constant.ZERO);
        SystemUserEntity systemUserEntity = iSystemUserService.getBaseMapper().selectOne(userEntity);
        return systemUserEntity;
    }


    /**
     * 根据用户名获取用户 对应的角色
     * @param roleName 用户名
     * @return
     */
    @Override
    public List<SystemRoleDto> getRole(String roleName) {
        return systemRoleMapper.getUserRole(roleName);
    }

    @Override
    public void updateOnline(String userName) {
        SystemUserEntity user = getUser(userName);
        user.setOnLine(1);
        iSystemUserService.updateById(user);
    }


}
